﻿/**  版本信息模板在安装目录下，可自行修改。
* Orders.cs
*
* 功 能： N/A
* 类 名： Orders
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2016/10/10 22:05:54   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
	/// <summary>
	/// 数据访问类:Orders
	/// </summary>
	public partial class Orders
	{
		public Orders()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("OrderId", "Orders"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int OrderId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Orders");
			strSql.Append(" where OrderId=@OrderId");
			SqlParameter[] parameters = {
					new SqlParameter("@OrderId", SqlDbType.Int,4)
			};
			parameters[0].Value = OrderId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Maticsoft.Model.Orders model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Orders(");
			strSql.Append("UserId,Total,OitemNum,OrderDesc,Layout,DeliveryFee,CouponFee,InsureFee,TaxFee,ProductPrice,PayTime,PayWayId,DeliveryTimeId,Name,sex,Phone,Mobile,LocationId,Address,PostCode,Status,TradeNo,CreateTime,UpdateTime,IsActive,ExpressId,ExpressNo,SourceId,OrderNo,IdentityNo,IdentityFrontPic,IdentityBackPic)");
			strSql.Append(" values (");
			strSql.Append("@UserId,@Total,@OitemNum,@OrderDesc,@Layout,@DeliveryFee,@CouponFee,@InsureFee,@TaxFee,@ProductPrice,@PayTime,@PayWayId,@DeliveryTimeId,@Name,@sex,@Phone,@Mobile,@LocationId,@Address,@PostCode,@Status,@TradeNo,@CreateTime,@UpdateTime,@IsActive,@ExpressId,@ExpressNo,@SourceId,@OrderNo,@IdentityNo,@IdentityFrontPic,@IdentityBackPic)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@Total", SqlDbType.Decimal,9),
					new SqlParameter("@OitemNum", SqlDbType.Int,4),
					new SqlParameter("@OrderDesc", SqlDbType.NVarChar,256),
					new SqlParameter("@Layout", SqlDbType.NVarChar,100),
					new SqlParameter("@DeliveryFee", SqlDbType.Decimal,9),
					new SqlParameter("@CouponFee", SqlDbType.Decimal,9),
					new SqlParameter("@InsureFee", SqlDbType.Decimal,9),
					new SqlParameter("@TaxFee", SqlDbType.Decimal,9),
					new SqlParameter("@ProductPrice", SqlDbType.Decimal,9),
					new SqlParameter("@PayTime", SqlDbType.DateTime),
					new SqlParameter("@PayWayId", SqlDbType.Int,4),
					new SqlParameter("@DeliveryTimeId", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@sex", SqlDbType.Int,4),
					new SqlParameter("@Phone", SqlDbType.VarChar,50),
					new SqlParameter("@Mobile", SqlDbType.VarChar,50),
					new SqlParameter("@LocationId", SqlDbType.Int,4),
					new SqlParameter("@Address", SqlDbType.NVarChar,200),
					new SqlParameter("@PostCode", SqlDbType.VarChar,50),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@TradeNo", SqlDbType.VarChar,100),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@UpdateTime", SqlDbType.DateTime),
					new SqlParameter("@IsActive", SqlDbType.Int,4),
					new SqlParameter("@ExpressId", SqlDbType.Int,4),
					new SqlParameter("@ExpressNo", SqlDbType.VarChar,50),
					new SqlParameter("@SourceId", SqlDbType.Int,4),
					new SqlParameter("@OrderNo", SqlDbType.VarChar,50),
					new SqlParameter("@IdentityNo", SqlDbType.VarChar,50),
					new SqlParameter("@IdentityFrontPic", SqlDbType.VarChar,50),
					new SqlParameter("@IdentityBackPic", SqlDbType.VarChar,50)};
			parameters[0].Value = model.UserId;
			parameters[1].Value = model.Total;
			parameters[2].Value = model.OitemNum;
			parameters[3].Value = model.OrderDesc;
			parameters[4].Value = model.Layout;
			parameters[5].Value = model.DeliveryFee;
			parameters[6].Value = model.CouponFee;
			parameters[7].Value = model.InsureFee;
			parameters[8].Value = model.TaxFee;
			parameters[9].Value = model.ProductPrice;
			parameters[10].Value = model.PayTime;
			parameters[11].Value = model.PayWayId;
			parameters[12].Value = model.DeliveryTimeId;
			parameters[13].Value = model.Name;
			parameters[14].Value = model.sex;
			parameters[15].Value = model.Phone;
			parameters[16].Value = model.Mobile;
			parameters[17].Value = model.LocationId;
			parameters[18].Value = model.Address;
			parameters[19].Value = model.PostCode;
			parameters[20].Value = model.Status;
			parameters[21].Value = model.TradeNo;
			parameters[22].Value = model.CreateTime;
			parameters[23].Value = model.UpdateTime;
			parameters[24].Value = model.IsActive;
			parameters[25].Value = model.ExpressId;
			parameters[26].Value = model.ExpressNo;
			parameters[27].Value = model.SourceId;
			parameters[28].Value = model.OrderNo;
			parameters[29].Value = model.IdentityNo;
			parameters[30].Value = model.IdentityFrontPic;
			parameters[31].Value = model.IdentityBackPic;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Maticsoft.Model.Orders model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Orders set ");
			strSql.Append("UserId=@UserId,");
			strSql.Append("Total=@Total,");
			strSql.Append("OitemNum=@OitemNum,");
			strSql.Append("OrderDesc=@OrderDesc,");
			strSql.Append("Layout=@Layout,");
			strSql.Append("DeliveryFee=@DeliveryFee,");
			strSql.Append("CouponFee=@CouponFee,");
			strSql.Append("InsureFee=@InsureFee,");
			strSql.Append("TaxFee=@TaxFee,");
			strSql.Append("ProductPrice=@ProductPrice,");
			strSql.Append("PayTime=@PayTime,");
			strSql.Append("PayWayId=@PayWayId,");
			strSql.Append("DeliveryTimeId=@DeliveryTimeId,");
			strSql.Append("Name=@Name,");
			strSql.Append("sex=@sex,");
			strSql.Append("Phone=@Phone,");
			strSql.Append("Mobile=@Mobile,");
			strSql.Append("LocationId=@LocationId,");
			strSql.Append("Address=@Address,");
			strSql.Append("PostCode=@PostCode,");
			strSql.Append("Status=@Status,");
			strSql.Append("TradeNo=@TradeNo,");
			strSql.Append("CreateTime=@CreateTime,");
			strSql.Append("UpdateTime=@UpdateTime,");
			strSql.Append("IsActive=@IsActive,");
			strSql.Append("ExpressId=@ExpressId,");
			strSql.Append("ExpressNo=@ExpressNo,");
			strSql.Append("SourceId=@SourceId,");
			strSql.Append("OrderNo=@OrderNo,");
			strSql.Append("IdentityNo=@IdentityNo,");
			strSql.Append("IdentityFrontPic=@IdentityFrontPic,");
			strSql.Append("IdentityBackPic=@IdentityBackPic");
			strSql.Append(" where OrderId=@OrderId");
			SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@Total", SqlDbType.Decimal,9),
					new SqlParameter("@OitemNum", SqlDbType.Int,4),
					new SqlParameter("@OrderDesc", SqlDbType.NVarChar,256),
					new SqlParameter("@Layout", SqlDbType.NVarChar,100),
					new SqlParameter("@DeliveryFee", SqlDbType.Decimal,9),
					new SqlParameter("@CouponFee", SqlDbType.Decimal,9),
					new SqlParameter("@InsureFee", SqlDbType.Decimal,9),
					new SqlParameter("@TaxFee", SqlDbType.Decimal,9),
					new SqlParameter("@ProductPrice", SqlDbType.Decimal,9),
					new SqlParameter("@PayTime", SqlDbType.DateTime),
					new SqlParameter("@PayWayId", SqlDbType.Int,4),
					new SqlParameter("@DeliveryTimeId", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@sex", SqlDbType.Int,4),
					new SqlParameter("@Phone", SqlDbType.VarChar,50),
					new SqlParameter("@Mobile", SqlDbType.VarChar,50),
					new SqlParameter("@LocationId", SqlDbType.Int,4),
					new SqlParameter("@Address", SqlDbType.NVarChar,200),
					new SqlParameter("@PostCode", SqlDbType.VarChar,50),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@TradeNo", SqlDbType.VarChar,100),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@UpdateTime", SqlDbType.DateTime),
					new SqlParameter("@IsActive", SqlDbType.Int,4),
					new SqlParameter("@ExpressId", SqlDbType.Int,4),
					new SqlParameter("@ExpressNo", SqlDbType.VarChar,50),
					new SqlParameter("@SourceId", SqlDbType.Int,4),
					new SqlParameter("@OrderNo", SqlDbType.VarChar,50),
					new SqlParameter("@IdentityNo", SqlDbType.VarChar,50),
					new SqlParameter("@IdentityFrontPic", SqlDbType.VarChar,50),
					new SqlParameter("@IdentityBackPic", SqlDbType.VarChar,50),
					new SqlParameter("@OrderId", SqlDbType.Int,4)};
			parameters[0].Value = model.UserId;
			parameters[1].Value = model.Total;
			parameters[2].Value = model.OitemNum;
			parameters[3].Value = model.OrderDesc;
			parameters[4].Value = model.Layout;
			parameters[5].Value = model.DeliveryFee;
			parameters[6].Value = model.CouponFee;
			parameters[7].Value = model.InsureFee;
			parameters[8].Value = model.TaxFee;
			parameters[9].Value = model.ProductPrice;
			parameters[10].Value = model.PayTime;
			parameters[11].Value = model.PayWayId;
			parameters[12].Value = model.DeliveryTimeId;
			parameters[13].Value = model.Name;
			parameters[14].Value = model.sex;
			parameters[15].Value = model.Phone;
			parameters[16].Value = model.Mobile;
			parameters[17].Value = model.LocationId;
			parameters[18].Value = model.Address;
			parameters[19].Value = model.PostCode;
			parameters[20].Value = model.Status;
			parameters[21].Value = model.TradeNo;
			parameters[22].Value = model.CreateTime;
			parameters[23].Value = model.UpdateTime;
			parameters[24].Value = model.IsActive;
			parameters[25].Value = model.ExpressId;
			parameters[26].Value = model.ExpressNo;
			parameters[27].Value = model.SourceId;
			parameters[28].Value = model.OrderNo;
			parameters[29].Value = model.IdentityNo;
			parameters[30].Value = model.IdentityFrontPic;
			parameters[31].Value = model.IdentityBackPic;
			parameters[32].Value = model.OrderId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int OrderId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Orders ");
			strSql.Append(" where OrderId=@OrderId");
			SqlParameter[] parameters = {
					new SqlParameter("@OrderId", SqlDbType.Int,4)
			};
			parameters[0].Value = OrderId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string OrderIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Orders ");
			strSql.Append(" where OrderId in ("+OrderIdlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.Orders GetModel(int OrderId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 OrderId,UserId,Total,OitemNum,OrderDesc,Layout,DeliveryFee,CouponFee,InsureFee,TaxFee,ProductPrice,PayTime,PayWayId,DeliveryTimeId,Name,sex,Phone,Mobile,LocationId,Address,PostCode,Status,TradeNo,CreateTime,UpdateTime,IsActive,ExpressId,ExpressNo,SourceId,OrderNo,IdentityNo,IdentityFrontPic,IdentityBackPic from Orders ");
			strSql.Append(" where OrderId=@OrderId");
			SqlParameter[] parameters = {
					new SqlParameter("@OrderId", SqlDbType.Int,4)
			};
			parameters[0].Value = OrderId;

			Maticsoft.Model.Orders model=new Maticsoft.Model.Orders();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.Orders DataRowToModel(DataRow row)
		{
			Maticsoft.Model.Orders model=new Maticsoft.Model.Orders();
			if (row != null)
			{
				if(row["OrderId"]!=null && row["OrderId"].ToString()!="")
				{
					model.OrderId=int.Parse(row["OrderId"].ToString());
				}
				if(row["UserId"]!=null && row["UserId"].ToString()!="")
				{
					model.UserId=int.Parse(row["UserId"].ToString());
				}
				if(row["Total"]!=null && row["Total"].ToString()!="")
				{
					model.Total=decimal.Parse(row["Total"].ToString());
				}
				if(row["OitemNum"]!=null && row["OitemNum"].ToString()!="")
				{
					model.OitemNum=int.Parse(row["OitemNum"].ToString());
				}
				if(row["OrderDesc"]!=null)
				{
					model.OrderDesc=row["OrderDesc"].ToString();
				}
				if(row["Layout"]!=null)
				{
					model.Layout=row["Layout"].ToString();
				}
				if(row["DeliveryFee"]!=null && row["DeliveryFee"].ToString()!="")
				{
					model.DeliveryFee=decimal.Parse(row["DeliveryFee"].ToString());
				}
				if(row["CouponFee"]!=null && row["CouponFee"].ToString()!="")
				{
					model.CouponFee=decimal.Parse(row["CouponFee"].ToString());
				}
				if(row["InsureFee"]!=null && row["InsureFee"].ToString()!="")
				{
					model.InsureFee=decimal.Parse(row["InsureFee"].ToString());
				}
				if(row["TaxFee"]!=null && row["TaxFee"].ToString()!="")
				{
					model.TaxFee=decimal.Parse(row["TaxFee"].ToString());
				}
				if(row["ProductPrice"]!=null && row["ProductPrice"].ToString()!="")
				{
					model.ProductPrice=decimal.Parse(row["ProductPrice"].ToString());
				}
				if(row["PayTime"]!=null && row["PayTime"].ToString()!="")
				{
					model.PayTime=DateTime.Parse(row["PayTime"].ToString());
				}
				if(row["PayWayId"]!=null && row["PayWayId"].ToString()!="")
				{
					model.PayWayId=int.Parse(row["PayWayId"].ToString());
				}
				if(row["DeliveryTimeId"]!=null && row["DeliveryTimeId"].ToString()!="")
				{
					model.DeliveryTimeId=int.Parse(row["DeliveryTimeId"].ToString());
				}
				if(row["Name"]!=null)
				{
					model.Name=row["Name"].ToString();
				}
				if(row["sex"]!=null && row["sex"].ToString()!="")
				{
					model.sex=int.Parse(row["sex"].ToString());
				}
				if(row["Phone"]!=null)
				{
					model.Phone=row["Phone"].ToString();
				}
				if(row["Mobile"]!=null)
				{
					model.Mobile=row["Mobile"].ToString();
				}
				if(row["LocationId"]!=null && row["LocationId"].ToString()!="")
				{
					model.LocationId=int.Parse(row["LocationId"].ToString());
				}
				if(row["Address"]!=null)
				{
					model.Address=row["Address"].ToString();
				}
				if(row["PostCode"]!=null)
				{
					model.PostCode=row["PostCode"].ToString();
				}
				if(row["Status"]!=null && row["Status"].ToString()!="")
				{
					model.Status=int.Parse(row["Status"].ToString());
				}
				if(row["TradeNo"]!=null)
				{
					model.TradeNo=row["TradeNo"].ToString();
				}
				if(row["CreateTime"]!=null && row["CreateTime"].ToString()!="")
				{
					model.CreateTime=DateTime.Parse(row["CreateTime"].ToString());
				}
				if(row["UpdateTime"]!=null && row["UpdateTime"].ToString()!="")
				{
					model.UpdateTime=DateTime.Parse(row["UpdateTime"].ToString());
				}
				if(row["IsActive"]!=null && row["IsActive"].ToString()!="")
				{
					model.IsActive=int.Parse(row["IsActive"].ToString());
				}
				if(row["ExpressId"]!=null && row["ExpressId"].ToString()!="")
				{
					model.ExpressId=int.Parse(row["ExpressId"].ToString());
				}
				if(row["ExpressNo"]!=null)
				{
					model.ExpressNo=row["ExpressNo"].ToString();
				}
				if(row["SourceId"]!=null && row["SourceId"].ToString()!="")
				{
					model.SourceId=int.Parse(row["SourceId"].ToString());
				}
				if(row["OrderNo"]!=null)
				{
					model.OrderNo=row["OrderNo"].ToString();
				}
				if(row["IdentityNo"]!=null)
				{
					model.IdentityNo=row["IdentityNo"].ToString();
				}
				if(row["IdentityFrontPic"]!=null)
				{
					model.IdentityFrontPic=row["IdentityFrontPic"].ToString();
				}
				if(row["IdentityBackPic"]!=null)
				{
					model.IdentityBackPic=row["IdentityBackPic"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select OrderId,UserId,Total,OitemNum,OrderDesc,Layout,DeliveryFee,CouponFee,InsureFee,TaxFee,ProductPrice,PayTime,PayWayId,DeliveryTimeId,Name,sex,Phone,Mobile,LocationId,Address,PostCode,Status,TradeNo,CreateTime,UpdateTime,IsActive,ExpressId,ExpressNo,SourceId,OrderNo,IdentityNo,IdentityFrontPic,IdentityBackPic ");
			strSql.Append(" FROM Orders ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" OrderId,UserId,Total,OitemNum,OrderDesc,Layout,DeliveryFee,CouponFee,InsureFee,TaxFee,ProductPrice,PayTime,PayWayId,DeliveryTimeId,Name,sex,Phone,Mobile,LocationId,Address,PostCode,Status,TradeNo,CreateTime,UpdateTime,IsActive,ExpressId,ExpressNo,SourceId,OrderNo,IdentityNo,IdentityFrontPic,IdentityBackPic ");
			strSql.Append(" FROM Orders ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Orders ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.OrderId desc");
			}
			strSql.Append(")AS Row, T.*  from Orders T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Orders";
			parameters[1].Value = "OrderId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

